/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uefs.sceca.vehicle;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author emanuel
 */
public class Vehicle implements Runnable{
    private int[][] intersection= new int[2][2];
    private static Socket car;
    private static ServerSocket server;
    private String Broadcast;
    private ObjectOutputStream out;
    
    public Vehicle(Socket socket) {
        this.car = socket;
    }
    
    public static void main(String[] args) {
            // TODO code application logic here
           try {
            // TODO code application logic here
            server = new ServerSocket(4010);
            
                while(true) {
                    Socket socket = server.accept();
                    Vehicle t = new Vehicle(socket);
                    t.run();
                }
        } catch (IOException ex) {
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        boolean answer = true;
        int code=0;
        DataInputStream input = null;
        while(answer){
            try {
                input = new DataInputStream(car.getInputStream());
                code = input.readInt();
            } catch (IOException ex) {
                Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
            }
            switch(code){
                case 1:
                    requisitionCrossing();
                case 2:
                    crossingIntersection();
            }
            
            
            
        }
    }
    
    public void start() {
        
        while (Broadcast== null || Broadcast.equals("")) {

                Broadcast = JOptionPane.showInputDialog(
                        "Digite o Endereço do Broadcast");
                System.out.println(Broadcast);
                
                connect(Broadcast, 4011);
           
            
                if (Broadcast == null || Broadcast.equals("")) {

                    JOptionPane.showMessageDialog(null,
                            "Você não digitou um endereço válido.");
                }
        }        
    }
    
    public void connect(String Broadcast, int port){
        try {
            car = new Socket(Broadcast, port);
            
        } catch (UnknownHostException ex) {
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void attemptCrossing(){
        try {
            /*
            __|X |__
               *  x           
            _x *  __ 
              | x|  
            */
            intersection[0][0]=1;
            intersection[0][1]=0;
            intersection[1][0]=1;
            intersection[1][1]=0;
            
            out = new ObjectOutputStream(car.getOutputStream());
            out.writeObject(intersection);
            out.flush();      
            
            
        } catch (IOException ex) {
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void setIntersection(int intersection[][]){
        
        this.intersection = intersection;
    }
    
    public int[][] getIntersection(){
        return intersection;
    }
    
    public void requisitionCrossing(){
        
        int[][] matrix = new int[2][2];
        try {
                ObjectInputStream input = new ObjectInputStream(car.getInputStream());
                matrix = (int[][]) input.readObject();
                
                
            } catch (IOException ex) {
                Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, null, ex);
            }
        for(int i=0; i<4; i++){            
            for(int j=0; i<4; i++){
                
                if(matrix[i][j]!=0 && intersection[i][j]==0){
                    intersection[i][j]=matrix[i][j];
                }
                if(matrix[i][j]!=0 && intersection[i][j]!=0){
                    System.out.println("Crossing Denied");
                }
            }
            
        }
    }
    public void printIntersection(){
        System.out.println("__|X |__\n" + "   *  x\n" + "_x *  __ \n" +"  | x|");
    }

    private void crossingIntersection() {
        
    }
    
}
